package self.xine.basic.thread.communication;

/**
 * 多线程间的通信，是通过共用的对象来作为桥梁实现的
 * 	
 *  而线程不安全，是指其在单线程内运行无误，
 *  	但在多线程中执行时由于未进行控制，极易引发共享数据错误问题 * 
 * 
 * @author xine
 * @date 2018年9月8日
 */
public class ThreadError implements Runnable {
	
	private int count = 10;
				
	@Override
	public void run() {
		while(count > 0) {
			
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			count--;
			System.out.println(Thread.currentThread().getName() + " : " + count);
		}
				
	}
	
	public static void main(String[] args) {
		ThreadError threadCommunication = new ThreadError();
		
		Thread t1 = new Thread(threadCommunication,"线程1");
		Thread t2 = new Thread(threadCommunication,"线程2");

		t1.start();
		t2.start();
				
		try {
			t1.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		try {
			t2.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println(threadCommunication.count);
		
	}

}
